package com.flyme.module.file.provider.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.Md5Sign;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysUserRemoteService;
import com.flyme.module.file.client.api.SysLiveRemoteService;
import com.flyme.module.file.client.entity.SysLive;
import com.flyme.module.file.provider.service.SysLiveService;
import com.flyme.module.file.provider.utils.PolyvUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "直播管理")
@RestController
public class SysLiveController extends BaseController<SysLiveService, SysLive> implements SysLiveRemoteService {
    @Autowired
    private SysUserRemoteService userRemoteService;

    /**
     * 直播授权
     */
    @ApiOperation(value = "直播授权", notes = "直播授权")
    @Override
    public Object auth(@RequestParam("userid") String userid, String ts, String token) {
        String sign = Md5Sign.sign(userid, ts);
        Map<String, Object> response = new HashMap<>();
        if (sign.equals(token)) {
            System.out.println("鉴权成功");
            response.put("status", 1);
            response.put("userid", userid);
            response.put("nickname", "测试yoghurt");
            response.put("marqueeName", "测试marqueeName");
            response.put("avatar", "http://live.polyv.net/assets/images/avatars/9avatar.jpg");
            response.put("actor", "VIP");
        } else {
            response.put("status", 0);
            response.put("errorUrl", "https://www.baidu.com");
            System.out.println("鉴权失败");
        }


        return response;
    }


    /**
     * 直播列表
     *
     * @return
     */
    @ApiOperation(value = "获取直播分页列表", notes = "获取直播分页列表")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysLive live) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("l.id,liveTitle,liveImage,liveContent,scene,channelId,liveStatus,regFee,liveTime,l.createTime,user.userName,isVipFree");
        cq.like("courseTitle", live.getLiveTitle());
        cq.ge("courseBeginDate", live.getStartDate());
        cq.le("courseBeginDate", live.getEndDate());
        EntityMap.setEnumConvertInterceptor(live);
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加直播", notes = "添加直播")
    @Override
    public ResultBody add(@ModelAttribute(value = "course") SysLive live) {
        live.setLiveStatus(CommonConstants.DISABLED);
        live.setIsVipFree(CommonConstants.DISABLED);
        boolean result = bizService.add(live);
        if (result) {
            return ResultBody.success(result);
        } else {
            return ResultBody.failed("创建失败");
        }

    }

    @ApiOperation(value = "通过courseId查询直播")
    @Override
    public ResultBody get(@PathVariable String id) {
        Map<String, Object> live = bizService.getMap(q().eq("id", id));
        List<Map<String, Object>> user = userRemoteService.getChosed("UT_LECTURER", live.get("lecturerId").toString()).getData();
        live.put("users", user);
        return ResultBody.success().setData(live);
    }

    @ApiOperation(value = "编辑直播", notes = "编辑直播")
    @Override
    public Callable<ResultBody> edit(SysLive live) {
        SysLive old = bizService.getById(live.getId());
        Callable<ResultBody> callable = () -> {
            if (!old.getLiveTitle().equals(live.getLiveTitle())) {
                //修改直播间名称
                PolyvUtil.updateChannel(live.getChannelId(), live.getLiveTitle());
            }
            return ResultBody.success(true);
        };
        bizService.edit(live);
        return callable;
    }

    @ApiOperation(value = "删除直播", notes = "删除直播")
    @Override
    public ResultBody delByIds(@RequestParam("ids") Long[] ids) {
        return bizService.delByIds(ids);
    }

    /**
     * 更新直播状态
     *
     * @param id         直播ID
     * @param liveStatus 是否上架
     * @return
     */
    @ApiOperation(value = "更新直播状态", notes = "更新直播状态")
    @Override
    public ResultBody setLiveStatus(@RequestParam("id") Long id, @RequestParam("liveStatus") Integer liveStatus) {
        boolean result = bizService.update(new UpdateWrapper<SysLive>().set("liveStatus", liveStatus).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 更新是否vip免费
     *
     * @param id 直播ID
     * @return
     */
    @ApiOperation(value = "更新是否vip免费", notes = "更新是否vip免费")
    public ResultBody setVipFree(@RequestParam("id") Long id, @RequestParam("isVipFree") Integer isVipFree) {
        boolean result = bizService.update(new UpdateWrapper<SysLive>().set("isVipFree", isVipFree).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }

}
